﻿using System;
using System.IO;
using System.Threading.Tasks;
using System.Windows;
using IntoMes.Global;
using Prism.Unity;
using Prism.Ioc;
using IntoMes.ViewModels;
using IntoMes.PLC;

namespace IntoMes
{
    /// <summary>
    /// App.xaml 的交互逻辑
    /// </summary>
    public partial class App : PrismApplication
    {
        protected override Window CreateShell()
        {
            ConfigCatalog();
            return base.Container.Resolve<MainWindow>();
        }

        protected override void RegisterTypes(IContainerRegistry _container)
        {
            Locator.Instance = new Locator(base.Container);
            //注册各个ViewModel
            _container.RegisterInstance(new Service());
            _container.RegisterInstance(new SparePartsManageViewModel());
            _container.RegisterInstance(new StationStatusViewModel());
            _container.RegisterInstance(new ProductionQueryViewModel());
            _container.RegisterInstance(new MainWindowViewModel());

            //挂载异常处理
            TaskScheduler.UnobservedTaskException += TaskScheduler_UnobservedTaskException;
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
        }


        private void TaskScheduler_UnobservedTaskException(object sender, UnobservedTaskExceptionEventArgs e)
        {

        }

        private void App_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            e.Handled = true;
        }

        private void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            var ex = e.ExceptionObject as Exception;
        }

        /// <summary>
        /// 配置文件目录
        /// </summary>
        private void ConfigCatalog()
        {
            Paths paths = new Paths();
            var dirs = typeof(Paths).GetProperties();
            foreach (var dir in dirs)
            {
                if (dir.Name.Contains("Dir"))
                {
                    if (Directory.Exists(dir.GetValue(paths).ToString()))
                        continue;
                    else
                        Directory.CreateDirectory(dir.GetValue(paths).ToString());
                }
            }
        }
    }
}
